#include <iostream>
#include <algorithm>
#include <sstream>
#include <cmath>
using namespace std;

int findHome(vector<int>& v){
	//n = middle index
	int n = (v.size()/2);
	//quickselect n'th larges element
	nth_element(v.begin(),v.begin()+n,v.end());
	//return
	return v[n];
}

int minDist(vector<int>& v){
	int addr = findHome(v);
	int totalDist = 0;
	for(int curr : v){
		totalDist += abs(addr-curr);
	}
	return totalDist;
}

int main(){
	int cases;
	cin >> cases;
	for(int i = 0; i < cases; ++i){
		//get line
		string input;

		if(cin.peek() == '\n')
			cin.get();
		
		//trash the first value its silly
		int g;
		cin >> g;
		
		//actually get a line
		getline(cin, input);
		//make the vector
		istringstream iss(input);
		int curr;
		vector<int> v;
		while(iss >> curr){
			v.push_back(curr);
		}
		//spit out the output
		cout << minDist(v) << endl;
	}
}
